package com.weetop.aklyscan.activity;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.wang.avi.AVLoadingIndicatorView;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.presenter.LoginPresent;

import java.io.UnsupportedEncodingException;

import ZPL.ZPLPrinterHelper;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class LianjieDialog extends Dialog {
    private Context mContext;
    private EditText edit;
    private TextView button, back, status;
    private ImageView dy_status;
    private AVLoadingIndicatorView avl;
    LoginPresent loginPresent;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                avl.hide();
                button.setEnabled(true);
                String sncode = "";
                try {
                    sncode = MainActivity.mZPL.getPrinterSN();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TextUtils.isEmpty(sncode)) {
                    ToastUtil.getInstance()._short(mContext, "SN号获取失败");
                    return;
                }
                String sn = sncode.replace("SERIAL NEMBER:", "");
                loginPresent.Into44(MainActivity.userInfoBean.getPrint_equipment(), edit.getText().toString().trim(), sn, "",
                        MainActivity.userInfoBean.getPrint_paper_width(), MainActivity.userInfoBean.getPrint_paper_height(),
                        MainActivity.userInfoBean.getPrint_deviation_x(), MainActivity.userInfoBean.getPrint_deviation_y());
                dismiss();
            } else if (msg.what == 2) {
                avl.hide();
                button.setEnabled(true);
            }
        }
    };

    public LianjieDialog(Context context, ImageView dy_status, TextView status, LoginPresent loginPresent) {
        super(context, R.style.promptDialog);
        setContentView(R.layout.dialog_lianjie);
        mContext = context;
        this.dy_status = dy_status;
        this.status = status;
        this.loginPresent = loginPresent;
        initVar();
        initView();
        initAttr();
        initAction();
    }

    public void initVar() {
    }

    public void initView() {
        edit = findViewById(R.id.edit);
        button = findViewById(R.id.button);
        back = findViewById(R.id.back);
        avl = findViewById(R.id.avl);
    }

    public void initAttr() {
        if (MainActivity.userInfoBean != null) {
            edit.setText(MainActivity.userInfoBean.getPrint_ip());
        }
    }

    public void initAction() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(edit.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(mContext, "输入内容不能为空");
                    return;
                }
                getDaYin();
                button.setEnabled(false);
                avl.show();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void getDaYin() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
//                    int i = MainActivity.mZPL.PortOpen("WiFi,192.168.43.114,9100");
                    int i = MainActivity.mZPL.PortOpen("WiFi," + edit.getText().toString().trim() + ",9100");
                    if (i == 0) {
                        ToastUtil.getInstance()._short(mContext, "连接成功");
                        MyApplication.dy_scaleIsConnect = false;
                        handler.sendEmptyMessage(1);
                    } else {
                        ToastUtil.getInstance()._short(mContext, "连接失败");
                        MyApplication.dy_scaleIsConnect = true;
                        handler.sendEmptyMessage(2);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        if (ZPLPrinterHelper.IsOpened()) {
//            status.setText("连接成功");
//            dy_status.setImageResource(R.mipmap.select_true);
//        } else {
//            status.setText("连接失败");
//            dy_status.setImageResource(R.mipmap.error);
//        }
    }

    private static String byteToString(byte[] bytes) {
        if (null == bytes || bytes.length == 0) {
            return "";
        }
        String strContent = "";
        try {
            strContent = new String(bytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return strContent;
    }
}
